package com.etc.order.frontend.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.etc.order.common.domain.ProductInfo;
import com.etc.order.common.domain.ShoppingCart;
import com.etc.order.common.domain.User;
import com.etc.order.common.dto.SimpleDTO;
import com.etc.order.common.enums.ProductStatusEnum;
import com.etc.order.common.service.ProductInfoService;
import com.etc.order.common.service.ShoppingCartService;
import com.etc.order.frontend.filter.AuthFilter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class ShoppingCartController {
    @Resource
    @Reference
    private ProductInfoService productInfoService;

    @Resource
    @Reference
    private ShoppingCartService shoppingCartService;

    @GetMapping("/cart/add")
    @ResponseBody
    public SimpleDTO addToCart(String productId, int productQuantity) {
        User user = (User) AuthFilter.getSession().getAttribute("userInfoFrontend");
        ProductInfo productInfo = productInfoService.getOne(productId);
        if (user != null && productInfo != null && productInfo.getProductStatus().intValue() != ProductStatusEnum.DOWN.getCode()) {
            ShoppingCart cart = new ShoppingCart();
            cart.setUid(user.getUid());
            cart.setProductId(productId);
            cart.setProductQuantity(productQuantity);
            int result = shoppingCartService.insertShoppingCart(cart);
            if (result >= 1) {
                return new SimpleDTO(0, "成功", null);
            }
        }
        return new SimpleDTO(1, "失败", null);
    }

    @GetMapping("/cart/deleteByProductId")
    @ResponseBody
    public SimpleDTO deleteFromCart(String productId) {
        User user = (User) AuthFilter.getSession().getAttribute("userInfoFrontend");
        ProductInfo productInfo = productInfoService.getOne(productId);
        if (user != null && productInfo != null) {
            if (shoppingCartService.deleteByUidAndProductId(user.getUid(), productId) > 0) {
                return new SimpleDTO(0, "成功", null);
            }
        }
        return new SimpleDTO(1, "失败", null);
    }

    @GetMapping("/cart/deleteByUid")
    @ResponseBody
    public SimpleDTO deleteFromCart() {
        User user = (User) AuthFilter.getSession().getAttribute("userInfoFrontend");
        if (user != null) {
            if (shoppingCartService.deleteByUid(user.getUid()) > 0) {
                return new SimpleDTO(0, "成功", null);
            }
        }
        return new SimpleDTO(1, "失败", null);
    }

    @GetMapping("/cart/update")
    @ResponseBody
    public SimpleDTO updateCart(String productId, int productQuantity) {
        User user = (User) AuthFilter.getSession().getAttribute("userInfoFrontend");
        ProductInfo productInfo = productInfoService.getOne(productId);
        if (user != null && productInfo != null && productInfo.getProductStatus().intValue() != ProductStatusEnum.DOWN.getCode()) {
            ShoppingCart cart = new ShoppingCart();
            cart.setUid(user.getUid());
            cart.setProductId(productId);
            cart.setProductQuantity(productQuantity);
            if (shoppingCartService.updateShoppingCart(cart) > 0) {
                return new SimpleDTO(0, "成功", null);
            }
        }
        return new SimpleDTO(1, "失败", null);
    }

    @GetMapping("/cart/list")
    @ResponseBody
    public SimpleDTO cartList() {
        User user = (User) AuthFilter.getSession().getAttribute("userInfoFrontend");
        List<ShoppingCart> carts;
        if (user != null) {
            carts = shoppingCartService.findByUid(user.getUid());
            return new SimpleDTO(0, "成功", carts);
        }
        return new SimpleDTO(1, "失败", null);
    }
}
